<?php

namespace App\Observers;

use App\Models\Constant\UserConstant;
use App\Models\Repair\RepairFactoryAdmin;
use App\Models\User\User;

class RepairFactoryAdminObserver
{
    /**
     * Handle the RepairFactoryAdmin "created" event.
     *
     * @param  \App\Models\Repair\RepairFactoryAdmin  $repairFactoryAdmin
     * @return void
     */
    public function created(RepairFactoryAdmin $repairFactoryAdmin)
    {
        $phone = $repairFactoryAdmin->phone;
        $user = User::where(compact('phone'))->first();
        if(!empty($user)){
            $user->type = UserConstant::TYPE_ALL;
            $user->save();
        }else{
            //自动给维修厂管理人员注册小程序用户
            $call_phone = $phone;
            $type = UserConstant::TYPE_REPAIR;
            $password = md5(env('INIT_PASSWORD','a7654321'));
            User::create(compact('phone','call_phone','password','type'));
        }
    }

    /**
     * Handle the RepairFactoryAdmin "updated" event.
     *
     * @param  \App\Models\Repair\RepairFactoryAdmin  $repairFactoryAdmin
     * @return void
     */
    public function updated(RepairFactoryAdmin $repairFactoryAdmin)
    {
        //
    }

    /**
     * Handle the RepairFactoryAdmin "deleted" event.
     *
     * @param  \App\Models\Repair\RepairFactoryAdmin  $repairFactoryAdmin
     * @return void
     */
    public function deleted(RepairFactoryAdmin $repairFactoryAdmin)
    {
        //
    }

    /**
     * Handle the RepairFactoryAdmin "restored" event.
     *
     * @param  \App\Models\Repair\RepairFactoryAdmin  $repairFactoryAdmin
     * @return void
     */
    public function restored(RepairFactoryAdmin $repairFactoryAdmin)
    {
        //
    }

    /**
     * Handle the RepairFactoryAdmin "force deleted" event.
     *
     * @param  \App\Models\Repair\RepairFactoryAdmin  $repairFactoryAdmin
     * @return void
     */
    public function forceDeleted(RepairFactoryAdmin $repairFactoryAdmin)
    {
        //
    }
}
